<?php

class Jf_View_Block_Tree_SubMenu extends Jf_View_Block_Tree_Menu {

    public function subMenu (Jf_Tree_Container $container = null, $options = null) {
        if (null !== $container) {
            $this -> setContainer ($container);
        }
        if (null !== $options) {
            foreach ($options as $key => $value) {
                $method = 'set' . ucfirst ($key);
                $this -> $method ($value);
            }
        }
        return $this;
    }

    public function renderSubMenu () {
        $container = $this -> getContainer ();
        $minDepth = $this -> getMinDepth ();
        $maxDepth = $this -> getMaxDepth ();
        if ( ! $active = $this -> findActive ($container, $minDepth, $maxDepth)) {
            return '';
        }
        if ($active['depth'] < $minDepth) {
            if ( ! $active['node'] -> hasNodes ()) {
                return '';
            }
        } elseif (is_int ($maxDepth) && $active['depth'] + 1 > $maxDepth) {
            $active['node'] = $active['node'] -> getParent ();
        }
        $iterator = new RecursiveIteratorIterator ($active['node'], RecursiveIteratorIterator::SELF_FIRST);
        $prevDepth =  - 1;
        $indent = '';
        $html = '';
        $ulClass = $this -> getUlClass ();
        foreach ($iterator as $node) {
            $depth = $iterator -> getDepth ();
            $myIndent = $indent . str_repeat ('        ', $depth);
            if ($depth < $minDepth ||  ! $this -> accept ($node)) {
                continue;
            }
            if ($depth > $prevDepth) {
                if ($ulClass && $depth == 0) {
                    $ulClass = ' class="' . $ulClass . '"';
                } else {
                    $ulClass = '';
                }
                $html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
            } else 
                if ($prevDepth > $depth) {
                    for ($i = $prevDepth; $i > $depth; $i -- ) {
                        $ind = $indent . str_repeat ('        ', $i);
                        $html .= $ind . '    </li>' . self::EOL;
                        $html .= $ind . '</ul>' . self::EOL;
                    }
                    $html .= $myIndent . '    </li>' . self::EOL;
                } else {
                    $html .= $myIndent . '    </li>' . self::EOL;
                }
            $liClass = $node -> isActive (true) ? ' class="active"' : '';
            $html .= $myIndent . '    <li' . $liClass . '>' . self::EOL;
            if ($minDepth === $depth && $node -> hasNodes ()) {
                $html .= $myIndent . '        ' . "<h3>" . $this -> htmlify ($node) . "</h3>" . self::EOL;
            } else {
                $html .= $myIndent . '        ' . $this -> htmlify ($node) . self::EOL;
            }
            $prevDepth = $depth;
        }
        for ($i = $prevDepth + 1; $i > 0; $i -- ) {
            $myIndent = $indent . str_repeat ('        ', $i - 1);
            $html .= $myIndent . '    </li>' . "\n" . $myIndent . '</ul>' . "\n";
        }
        return $html;
    }

    public function render () {
        return $this -> renderSubMenu ();
    }

}
